package com.codeyang.mocktest2.service.impl;

import com.codeyang.mocktest2.entity.TUser;
import com.codeyang.mocktest2.mapper.TUserMapper;
import com.codeyang.mocktest2.service.FileLastingService;
import com.codeyang.mocktest2.service.ResourceService;
import com.codeyang.mocktest2.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: describe
 *
 * @author shengyang5
 * @version 2021/7/20 16:44
 */
@Service
public class TUserServiceImpl implements TUserService {

    @Resource
    private TUserMapper tUserMapper;


    @Override
    public TUser selectByPrimaryKey(Integer id) {
        return tUserMapper.selectByPrimaryKey(id);
    }


    @Override
    public TUser saveUser(TUser tUser) {
        int insert = tUserMapper.insert(tUser);
        if (insert > 0) {
            if (!ObjectUtils.isEmpty(tUser.getId())) {
                return tUserMapper.selectByPrimaryKey(tUser.getId());
            }
        }
        return null;
    }

    @Override
    public List<TUser> queryAll() {
        return tUserMapper.selectAll();
    }

    /**
     * 根据name删除用户
     * 1:name not unique 先找到name对应的ids
     * 2:集中删除
     *
     * @param name
     * @return
     */
    @Override
    public int delByName(String name) {
        List<Integer> ids = tUserMapper.selectByName(name);
        if (!CollectionUtils.isEmpty(ids)) {
            return tUserMapper.delByIds(ids);
        }
        return 0;
    }


    /**
     * 修改用户step
     * 1：先根据id查找用户是否存在
     * 2：存在就修改
     *
     * @param tUser
     * @return
     */
    @Override
    public int updateUser(TUser tUser) {
        if (!ObjectUtils.isEmpty(tUser) && tUser.getId() != null) {
            if (tUserMapper.selectByPrimaryKey(tUser.getId()) == null) {
                return 1;
            }
            return tUserMapper.updateObj(tUser);

        }
        return 0;
    }

    @Override
    public List<TUser> queryAddressAndName(String address, String name) {
        List<TUser> list = tUserMapper.selectByNames(name);
        List<TUser> collect = list
                .stream()
                .filter(tUser -> tUser.getAddress().endsWith(address))
                .collect(Collectors.toList());
        return collect;
    }

    // 如果此时 资源名称服务和文件服务都还没有开发完成呢？接口先约定好了
    // 假设这两个服务是远程调用的，很卡什么的
    @Autowired
    private ResourceService resourceService;

    @Autowired
    private FileLastingService fileLastingService;

    @Override
    public String accpetFile(MultipartFile file) throws Exception {
        if (ObjectUtils.isEmpty(file.isEmpty())) {
            return "文件上传失败";
        }

        // 处理逻辑 -- 计算得到唯一一个文件名称 -- 依赖资源服务
        String newFileName = resourceService.calculationFile(file.getOriginalFilename());
        // 处理逻辑 存储上传的文件  -- 依赖文件持久服务
        String content = fileLastingService.fileWriteLoadContent(file, newFileName);

        return content;
    }
}
